/**
 * 绑定银行卡
 */
var bank = function(){

    var countdown = 60;

    var bankSubmit = function(){
        $('#bank-form').submit(function(e) {
            e.preventDefault();
        }).validate({
            rules: {
                bank_id: {
                    required : true,
                    digits : true
                },
                cardholder: {
                    required: true,
                    chinese : true
                },
                pin: {
                    required : true,
                    checkcard: true
                },
                company : {
                    minlength : 4
                },
                bank_name : {
                    minlength : 4
                },
                company_bank : {
                    minlength : 6,
                    digits : true
                },
                bank_card: {
                    required : true,
                    checkBankCard : true
                },
                mobile_phone: {
                    required : true,
                    mobile :true
                },
                captcha : {
                    required :true,
                    maxlength : 6,
                    minlength : 6,
                    digits : true
                }
            },
            messages: {
                bank_id : {
                    required : '请选择银行卡号',
                    digits : '请选择银行卡号'
                },
                cardholder: {
                    required: '请输入持卡人姓名',
                    chinese : '请与持卡人姓名一致'
                },
                pin: {
                    required : '请输入身份证号码',
                    checkcard: '请输入正确的身份证号码'
                },
                bank_card:{
                    required : '请输入银行卡卡号',
                    checkBankCard :'银行卡卡号不合法'
                },
                company : {
                    minlength : '请输入正确的公司名称',
                },
                bank_name : {
                    minlength : '请输入正确的银行名称',
                },
                company_bank : {
                    minlength : '请输入正确的对公帐号',
                    digits    : '请输入正确的对公帐号'
                },
                mobile_phone: {
                    required : '请输入您的手机号码',
                    mobile : '手机号码格式不正确'
                },
                captcha : {
                    required :'请输入短信效验码',
                    maxlength : '请输入6位短信效验码',
                    minlength : '请输入6位短信效验码',
                    digits : '请输入6位短信效验码'
                }
            },
            errorPlacement: function(e, el) {
                $(el).next().text(e.text());
            },
            success: function(e, el) {
                $(el).next().text(e.text());
            },
            submitHandler: function(f) {
                $.ajax({
                    type: 'post',
                    async: true,
                    dataType : 'json',
                    url: hostUrl() +'/userBank/addPost',
                    data: $('#bank-form').serialize(),
                    beforeSend: function() {
                        $('#bank-form button[type=submit]').text('提交中...').attr('disabled', true);
                    },
                    success: function(data) {
                        if (data.status) {
                            window.location.href = data.msg;
                        } else {
                            alert(data.msg);
                            $('#bank-form button[type=submit]').text('提交').removeAttr('disabled');
                        }
                    }
                });

                return false;
            }
        });
    };

    /**
     * 个人账户
     */
    $('.bank-user-type-1').click(function(){
        $('.bank-type-1').removeClass('my-hide');
        $('.bank-type-2').addClass('my-hide');
        $('.bank-type-2').find('input').val('');
    });

    /**
     * 对公账户
     */
    $('.bank-user-type-2').click(function(){
        $('.bank-type-2').removeClass('my-hide');
        $('.bank-type-1').addClass('my-hide');
        $('.bank-type-1').find('input').val('');
    });

    /**
     * 发送短信，短信初始化化，从60秒
     * @type {{countdown: number, setTime: Function}}
     */
    var setTime = function(obj) {
        if (countdown == 0) {
            obj.removeClass("disabled");
            obj.text("免费获取");
            countdown = 60;
        } else {
            obj.addClass("disabled");
            obj.text('重新发送(' + countdown + ')');
            countdown--;
            setTimeout(function() {
                setTime(obj);
            }, 1000);
        }
    }

    /**
     * 发送短信
     */
    $('#get-bank-mobile-phone-captcha').click(function(){
        var mobilePhone = $('input[name=mobile_phone]').val();
        var obj = $(this);
        if (mobilePhone.length == 11) {
            $.ajax({
                type: 'post',
                async: false,
                dataType : 'json',
                url: hostUrl() + '/validate/ajaxSendCaptcha',
                data : {mobilePhone:mobilePhone},
                beforeSend : function(){},
                success : function(data) {
                    if (data.status) {
                        setTime(obj);
                    } else {
                        $('input[name=mobile_phone]').next('.error').text(data.msg);
                    }
                }
            });
        } else {
            $('input[name=mobile_phone]').next('.error').text('请输入正确的手机号码');
        }
    });

    return {
        init : function() {
            bankSubmit();
            $('.add-text').on('click','.take-box',function(event){
                $('.bank-react').toggle();
                event.stopPropagation();
            });
            $('.take-box').on('click','.bank-react li',function(event){
                var png = $(this).find('img').attr('src');
                var bankId = $(this).data('id');
                console.log(bankId);
                $('#tank-img').attr("src",png);
                $('.bank-react').hide();
                $('input[name=bank_id]').val(bankId);
                event.stopPropagation();
            });
            $('body').on('click',function(event){
                $('.bank-react').hide();
                event.stopPropagation();
            });
        }
    }
}();

jQuery(function(){ bank.init(); });